package io.renren.controller;

import com.alibaba.fastjson.JSONObject;
import io.renren.annotation.LoginUser;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.dto.AuxiliaryMateriDTO;
import io.renren.dto.AuxiliaryMaterialsmxDTO;
import io.renren.dto.TemporaryOrderDTO;
import io.renren.entity.UserEntity;
import io.renren.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;


/**
 * 辅材采购表
 *
 * @author 罗胜平 781757616@qq.com
 * @since 1.0.0 2019-11-22
 */
@RestController
@RequestMapping("api/auxiliarymateri")
@Api(tags="辅材采购")
public class AuxiliaryMateriController {
    @Autowired
    private AuxiliaryMateriService auxiliaryMateriService;
    @Autowired
    private AuxiliaryMaterialsmxService auxiliaryMaterialsmxService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private TemporaryOrderService temporaryOrderService;
    @GetMapping("page")
    @ApiOperation("仓管员辅材申请首页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<Map> page(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity){
        PageData<AuxiliaryMateriDTO> page = auxiliaryMateriService.getOrder(params,entity);
        TemporaryOrderDTO temOrder = temporaryOrderService.getTemOrder(1, entity.getId(), entity.getDeptId());
        ArrayList<AuxiliaryMateriDTO> arraylist = new ArrayList<>();
        if(null == params.get("page") && null != temOrder){
            AuxiliaryMateriDTO materiDTO = new AuxiliaryMateriDTO();
            materiDTO.setCreateDate(temOrder.getCreateTime());
            materiDTO.setFlag(1);
            String deptName = sysDeptService.getDeptName(entity.getDeptId()).getName()+"主食进货清单草稿";
            materiDTO.setChedRemake(deptName);
            arraylist.add(materiDTO);
        }
        HashMap<String, Object> map = new HashMap<>();
        for (AuxiliaryMateriDTO materiDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(materiDTO.getCanteenId()).getName()+"主食进货清单";
            materiDTO.setChedRemake(deptName);
            arraylist.add(materiDTO);
        }
        page.setList(arraylist);
        map.put("list",page);
        AuxiliaryMateriDTO total = auxiliaryMateriService.getTotal(entity.getId(), null, entity.getDeptId(), 3);
        map.put("pending",total.getPending());
        return new Result<Map>().ok(map);
    }
    /**
     * 辅材采购订单
     * @return
     */
    @PostMapping("saveAuxiliaryOrder")
    @ApiOperation("辅材采购订单")
    public Result saveAuxiliaryOrder(@RequestBody JSONObject params, @LoginUser UserEntity entity){
        auxiliaryMateriService.saveAuxiliaryOrder(params,entity);
        return new Result<>();
    }


    /**
     * 根据供应商id和订单id获取订单详情
     * @param supplierId
     * @param orderId
     * @return
     */
    @GetMapping("/selecSupplierOrder1")
    @ApiOperation("供应商订单详情信息")
    public Result<List<AuxiliaryMaterialsmxDTO>> selecSupplierOrder(Long supplierId, Long  orderId ){
        List<AuxiliaryMaterialsmxDTO> data = auxiliaryMaterialsmxService.selectFoods(orderId,supplierId);
            return new Result<List<AuxiliaryMaterialsmxDTO>>().ok(data); //
    }

    @GetMapping("pageAdmin")
    @ApiOperation("食堂管理员查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
    })
    public Result<Map> pageAdmin(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<AuxiliaryMateriDTO> page = auxiliaryMateriService.pageAdmin(params);
        HashMap<String, Object> map = new HashMap<>();
        for (AuxiliaryMateriDTO materiDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(materiDTO.getCanteenId()).getName()+"主食采购清单";
            materiDTO.setChedRemake(deptName);
        }
        map.put("list",page);
        AuxiliaryMateriDTO total = auxiliaryMateriService.getTotal(null, null, null, 1);
        map.put("pending",total.getPending());
        return new Result<Map>().ok(map);
    }
    @GetMapping("pageChef")
    @ApiOperation("食堂主厨查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
    })
    public Result<Map> pageChef(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity){
        PageData<AuxiliaryMateriDTO> page = auxiliaryMateriService.pageChef(params);
        HashMap<String, Object> map = new HashMap<>();
        for (AuxiliaryMateriDTO materiDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(materiDTO.getCanteenId()).getName()+"主食采购清单";
            materiDTO.setChedRemake(deptName);
        }
        map.put("list",page);
        AuxiliaryMateriDTO total = auxiliaryMateriService.getTotal(entity.getId(), null, entity.getDeptId(), 4);
        map.put("pending",total.getPending());
        return new Result<Map>().ok(map);
    }
    @PostMapping("pageAdminUpdate")
    @ApiOperation("食堂管理员审核")
    public Result updateExam(@RequestBody AuxiliaryMateriDTO dto, @LoginUser UserEntity entity){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        return  auxiliaryMateriService.updateExamine(dto,entity);
    }
    @PostMapping("UpdateSupplierOrder")
    @ApiOperation("供应商修改订单状态")
    public Result UpdateSupplierOrder(@RequestBody AuxiliaryMateriDTO dto, @LoginUser UserEntity entity){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        return  auxiliaryMateriService.UpdateSupplierOrder(dto,entity);
    }
    @GetMapping("/selecSupplierMateriOrder")
    @ApiOperation("供应商的订单列表")
    public Result<Map> selecSupplierMateriOrder(Long supplierId ) {
         Map map = auxiliaryMateriService.selecSupplierMateriOrder(supplierId);
        return new Result<Map>().ok(map);
    }
}